﻿var items;

$(document).ready(function () {
    if ($.cookie("userS") != "Admin") {
        $(".submenu").hide();
    }
    if (window.location.href.indexOf("?selectedItem=myGoals") < 0 && window.location.href.indexOf("?selectedItem=allGoals") < 0) {
        window.location.href = "Default.aspx?selectedItem=myGoals";
    }
    if (window.location.href.indexOf("?selectedItem=myGoals") >= 0) {
        $("#myGoals a").css({ 'color': '#8EBD00' });
        $(".hTxt").text("Home > My Goals");
        initMyHomePage($.cookie("userId"));
    }
    else if (window.location.href.indexOf("?selectedItem=allGoals") >= 0) {
        $("#allGoals a").css({ 'color': '#8EBD00' });
        $(".hTxt").text("Home > All Goals");
        initAllHomePage($.cookie("userId"));
    }
});

function setInfo() {
    
}

function addNewGoal() {
    
}

function initPageData(data, admin) {
    if (data.Status == "success") {
        items = $.parseJSON(data.Message);
        drawItems();
    } else {
        if (admin) {
            //window.location.href = "Default.aspx?selectedItem=myGoals";
        }
    }
}

function drawItems() {
    for (var i = 0; i < items.length; i++) {
        $('<tr/>', {
            id: 'tr-' + i,
            class: "td " + (((i + 1) % 2) ? "" : "odd")
        }).appendTo('#mainTable');

        $('<td/>', {
            id: 'tdNumber-' + i,
            text: i + 1
        }).appendTo('#tr-' + i).css({ 'text-align': 'center' });

        $('<td/>', {
            id: 'tdGoal-' + i,
            text: items[i].Goal.Name
        }).appendTo('#tr-' + i);

        $('<td/>', {
            id: 'tdTasks-' + i,
        }).appendTo('#tr-' + i);

        for (var j = 0; j < items[i].Task.length; j++) {
            $('<div/>', {
                id: 'task-' + j,
                text: items[i].Task[j].Name
            }).appendTo('#tdTasks-' + i);
        }
    }
}